<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template_view extends CI_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Projectmodel','m_project');
		$this->load->model('Templatemodel','m_template');
		$this->load->model('Widgetmodel','m_widget');
		$this->load->model('Frameworkmodel','m_framework');
		$this->load->model('Codemodel','m_code');
		$this->load->database();
	}

	public function __destruct() {
		$this->db->close();
	}


	public function index($template_id = false,$show_template_list = false){
		if($template_id === false) show_404();
		$template = $this->m_template->getTemplate($template_id);

		if(empty($template)) show_404();
		$project = $this->m_project->getProject($template['project_id']);
		if(empty($project)) show_404();
		$data['project'] = $project;

		if(!validate($project['project_commonfile'])) $project['project_commonfile'] = '[]';

		$project_id = $project['project_id'];
		$proj_css = $this->m_project->getProjectCss($project_id);
		$project_css_content = empty($proj_css)?'':$proj_css['project_css_content'];
		$project_css_content = $project_css_content;
		$proj_js = $this->m_project->getProjectJs($project_id);
		$project_js_content = empty($proj_js)?'':$proj_js['project_js_content'];

		//add code js css by klj 2014-06-11
		$code_list = json_decode($project['project_code_ids'],true);
		if(!empty($code_list)) {
			$code_list = $this->m_code->getCodeByIds($code_list);
		}
		$code_css = '';
		$code_js = '';
		foreach ($code_list as $key => $value) {
			$code_css .= $value['code_content_css'];
			$code_js .= $value['code_content_js'];
		}
		$project_js_content = $code_js.$project_js_content;
		$project_css_content = $code_css.$project_css_content;

		$widget_list = $this->m_widget->getWidgetListByStruct($template['template_struct'],$template['project_id']);
		$widget_list = reindexArray($widget_list,'widget_id');

		global $compression_replace;
		$data['project_id'] = $project['project_id'];
	
		$data['project_type'] = $project['project_type'];
		$data['common_file'] = json_decode($project['project_commonfile']);

		$data['template_css'] = '';
		$data['template_html'] = $template['template_struct'];
		foreach ($widget_list as $widget) {
			$data['template_css'] .= $widget['widget_css'];
			if(validate($widget['widget_js'])){
				//insert widget js in widget html;
				$widget['widget_html'] = str_replace("<!-- WIDGET-".$widget['widget_name']."-EOF -->",'',$widget['widget_html']); 
				$widget['widget_html'] .= '<script type="text/javascript">'.$widget['widget_js'].'</script>';
				$widget['widget_html'] .= "\n<!-- WIDGET-".$widget['widget_name']."-EOF -->";
			}
			$data['template_html'] = str_replace('<widget>'.$widget['widget_name'].'</widget>',$widget['widget_html'],$data['template_html']);
		}

		if($show_template_list){
			$template_list = $this->m_template->getTemplateListByProject($project_id);
			$str = '';
			foreach ($template_list as $k=>$v) {
				$str .= '<a style="display:block;margin:3px;'.(($template_id==$v['template_id'])?'color:#F00;':'color:#FFF').'" href="'.base_url().'t/'.$v['template_id'].'/1" title="'.$v['template_title'].'">'.$v['template_name'].' #'.$v['template_id'].'</a>';
			}

			$data['template_html'] .= '<div style="position:fixed;max-height:650px;overflow:auto;top:0;right:20px;padding:10px;background: rgba(0, 0, 0, 0.3);">'.$str.'</div>';
		}
		$data['template_css'] .= $template['template_css'];
		$data['template_css'] = str_replace($compression_replace,'',$data['template_css']);

		$this->load->library('codecompression');
		$project_js_content = $this->codecompression->jscompression($project_js_content);
		$project_css_content = $this->codecompression->csscompression($project_css_content);

		$data['project_js'] = $project_js_content;
		$data['project_css'] = $project_css_content;
		$data['project_code'] = $project['project_code'];
		$data['title'] = $project['project_name'].' - '. $template['template_name'];

		if(!empty($template['template_title'])){
			$data['title'] = $template['template_title'];
		}


		
		$this->load->view('template_view',$data);
	}

	public function getProjectCss($project_id = false){
		header("Content-type: text/css; charset: UTF-8");
		$item = array();
		$res = '';
		if($project_id !== false) {
			$item = $this->m_project->getProjectCss($project_id);
		}
		if(!empty($item)){
			$res .= ' '.$item['project_css_content'];
		}
		echo $res;
	}

	public function getProjectJs($project_id = false){
		header("Content-type: text/javascript; charset: UTF-8");
		$item = array();
		if($project_id !== false) {
			$item = $this->m_project->getProjectJs($project_id);
		}
		if(!empty($item)){
			echo $item['project_js_content'];
		}
	}
}
/* End of file template_view.php */
/* Location: ./application/controllers/template_view.php */